package com.you.raft.common.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Getter
@Setter
@ToString
public class Cluster {


    private List<Node> list = new ArrayList<>();
    private volatile List<Node> others = new ArrayList<>();
    private volatile Node leader;
    private volatile Node self;
    private volatile int version = 0;
    private volatile int othersVersion = 0;

    public Cluster() {
    }

    /**
     * 获取其他的节点信息
     *
     * @return
     */

    public List<Node> getOtherNodes() {
        if (othersVersion != version) {
            others = new ArrayList<>(list);
            others.remove(self);
            othersVersion = version;
        }
        return others;
    }

    public void addNode(String address) {
        list.add(new Node(address));
        version++;
    }
}
